In computer systems, there is often a need to manage memory. In particular, when objects are no longer used, it is useful to recover memory that was allocated for use with the objects. Some systems in use today use explicit memory management whereby the systems use a command to allocate memory and a corresponding command to free memory. For example, the C programming language includes a command “malloc” that can be used to allocate memory for an object and a command “free” that can be used to free memory that has been previously allocated. However, this type of memory allocation and de-allocation suffers from a number of drawbacks. In particular, free memory may be scattered throughout a memory structure. Additionally, this type of allocation and de-allocation allows for malicious attacks that can be used to compromise data. For example, a hacker can cause memory that has been allocated by one thread to be de-allocated such that a malicious thread can change or access data stored in the memory.
To combat these drawbacks, a system has been created using automatic memory management where the system includes garbage collectors for identifying objects that are no longer being used. Memory for the objects no longer being used can be identified as free memory such that the memory can be used for the creation of new objects. Typical garbage collection involves marking objects that can be reached beginning at a reference by a root and reclaiming memory for any objects that are not marked by sweeping the heap. Marking can occur by setting (or clearing) a flag (such as a single bit) in the object. After sweeping the heap, all flags for the objects are cleared (or set) such that subsequent garbage collection can take place.
To perform marking activities, a root provides a reference to one or more in use objects. These objects are visited and marked. References from these objects to other objects are then followed to the other objects. The other objects are marked and their references to still other objects are followed until all of the in use objects have been marked.
To facilitate marking, a mark stack may be employed. The mark stack allows references at one object to other objects to be entered on the mark stack. When the mark stack is limited in size, the mark stack may overflow. For example, if an object is visited that has 12 references and the mark stack only has 10 free entries, an overflow is determined to have occurred and none of the references to the 12 referenced objects are placed on the mark stack.
Typically, when an overflow occurs, a heap including the objects is examined linearly by examining memory to determine if an object has been marked, and if it has been marked by following references in the object and marking the objects referenced by the references. While it may not be necessary to examine the entire heap as ranges of overflows can be implemented, as can be appreciated, this can nonetheless result in a time consuming and resource intensive marking process.
The subject matter claimed herein is not limited to embodiments that solve any disadvantages or that operate only in environments such as those described above. Rather, this background is only provided to illustrate one exemplary technology area where some embodiments described herein may be practiced.